package com.imessage.action.business.sendtask;

import com.bean.BusinessGroup;
import com.bean.BusinessTask;
import com.bean.BusinessTaskDetail;
import com.imessage.comm.Constants;
import com.imessage.jackson.JsonObject;
import com.imessage.type.BaseState;
import com.imessage.utils.StrUtil;
import com.imessage.vo.TaskVo;
import com.imessage.web.Page;
import io.ebean.Expr;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/admin/sendtask")
public class SendTaskAction extends SendTaskBaseAction {


    /**
     * 列表数据,搜索数据
     */
    @RequestMapping(value = "")
    public Object run(TaskVo taskVo) {
        List<Expression> expressionList = new ArrayList<>();
        setExpressionList(expressionList, taskVo);
        Page<BusinessTask> page = baseService.findPage(BusinessTask.class, expressionList,
                null, getOrder(taskVo.getOrders()), taskVo.getPageCurrent(),
                taskVo.getPageSize());
        return page;
    }

    /**
     * 添加修改界面
     */
    @RequestMapping(value = "/initpage")
    public ModelAndView initpage(
            @RequestParam(name = "req_id", required = true) String id) {
        ModelAndView modelAndView = new ModelAndView();
        BusinessTask task = new BusinessTask();
        if (StringUtils.isNotBlank(id)) {
            task = baseService.findById(BusinessTask.class, id);
            modelAndView.setViewName("admin/business/sendtask/modify");
        } else {
            List<Expression> expressionList = new ArrayList<>();
            expressionList.add(Expr.eq("state", BaseState.normal.getCode()));
            List<BusinessGroup> businessGroupList=baseService.findList(
                    BusinessGroup.class, expressionList);
            modelAndView.addObject("groupList", businessGroupList);
            modelAndView.setViewName("admin/business/sendtask/add");
        }
        setModelAndView(modelAndView, task);
        return modelAndView;
    }


    /**
     * 添加
     */
    @RequestMapping(value = "/add")
    public Object add(TaskVo taskVo,MultipartFile img1file) {
        String realPath=String.format("%s%s%s",
                StrUtil.getOsResourceLoc(environment),File.separator,"task");
        File fileDir = new File(realPath);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        String dir=UUID.randomUUID().toString().replaceAll("-", "");
        realPath=realPath+File.separator+dir;
        fileDir = new File(realPath);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        BusinessGroup businessGroup=null;
        if(StringUtils.isNotBlank(taskVo.getBusinessGroupId())){
            businessGroup=baseService.findById(BusinessGroup.class, taskVo.getBusinessGroupId());
        }
        String imgName= fileUpload(img1file,realPath,"1",false);
        int state=taskService.saveBusinessTaskFromAdd(
                businessGroup,taskVo.getMsg(),imgName,realPath);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("任务提交成功", datagrid);
        }
        return getJsonForErr("任务失败", datagrid);
    }


    /**
     * 详情页
     */
    @RequestMapping(value = "/detail")
    public Object details(TaskVo taskVo) {
        List<Expression> expressionList = new ArrayList<>();
        if(taskVo.getId()!=null&&taskVo.getId().intValue()>0){
            expressionList.add(Expr.eq("BusinessTask.id", taskVo.getId()));
        }
        Page<BusinessTaskDetail> page = baseService.findPage(BusinessTaskDetail.class, expressionList,
                null, getOrder(taskVo.getOrders()), taskVo.getPageCurrent(),
                taskVo.getPageSize());
        return page;
    }

    /**
     * 详情页
     */
    @RequestMapping(value = "/initdetail")
    public ModelAndView initdetail(@RequestParam(name="id",required = true)long id) {
        ModelAndView modelAndView = new ModelAndView();
        JsonObject jsonObject=new JsonObject();
        jsonObject.put("height", "100%");
        jsonObject.put("showToolbar", true);
        jsonObject.put("toolbarItem", "refresh,|,del");
        jsonObject.put("dataUrl", "/admin/sendtask/detail?id="+id);
        jsonObject.put("delUrl", "/admin/sendtask/deldetail");
        jsonObject.put("delPK", "id");
        jsonObject.put("delConfirm", true);
        jsonObject.put("dataType", "json");
        jsonObject.put("inlineEditMult", false);
        jsonObject.put("linenumberAll", true);
        jsonObject.put("filterThead", false);
        jsonObject.put("hScrollbar", true);
        modelAndView.setViewName("admin/business/sendtask/detail");
        modelAndView.addObject("tableOptions", jsonObject);
        return modelAndView;
    }




    /**
     * 删除
     */
    @RequestMapping(value = "/del")
    public Object del(
            @RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            BusinessTask task = baseService.findById(BusinessTask.class, id);
            state = baseService.delObject(task);
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/deldetail")
    public Object deldetail(
            @RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            BusinessTaskDetail detail = baseService.findById(BusinessTaskDetail.class, id);
            state = baseService.delObject(detail);
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }


}
